package top.psjj.ssm.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

/**
 * 2.解释这个类的作用以及每个方法的作用，共3分
 * spring整合Mybatis配置类
 *
 * SqlSessionFactoryBean
 * 配置数据库练连接的工厂
 * 把数据源和mybatis映射的实体类通过set方法传给工厂
 *
 * MapperScannerConfigurer
 *配置接口和映射的文件扫描器
 */
public class MybatisConfig {

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("top.psjj.ssm.entity");
        return factoryBean;
    }
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("top.psjj.ssm.mapper");
        return msc;
    }
}
